﻿Imports System.IO

Module TeleTu
    Public Function getWpa_teletu(ByVal mac As String)
        Dim wpa_teletu As String = ""
        Dim tab_teletu(0) As String
        Dim pos As Short = 0
        mac = UCase(Replace(mac, ":", ""))
        Dim sr As New StringReader(My.Resources.Tabella_Teletu)
        Dim riga_tab_teletu = sr.ReadLine
        Do Until riga_tab_teletu = Nothing
            ReDim Preserve tab_teletu(pos)
            tab_teletu(pos) = riga_tab_teletu.Replace(":", "")
            pos += 1
            riga_tab_teletu = sr.ReadLine
        Loop
        For pos = 0 To tab_teletu.GetUpperBound(0)
            If mac.Substring(0, 6) = tab_teletu(pos).Substring(0, 6) Then
                If mac.Substring(6, 6) >= tab_teletu(pos).Substring(7, 6) And mac.Substring(6, 6) <= tab_teletu(pos).Substring(14, 6) Then
                    Dim base As Integer = Val("&h" & tab_teletu(pos).Substring(27, 6))
                    Dim Sn1 As String = tab_teletu(pos).Substring(21, 5)
                    Dim div As Byte = tab_teletu(pos).Substring(34, 1)
                    Dim tsn2 As Integer = Val("&h" & mac.Substring(6, 6)) - base
                    Dim sn2 As String = CStr(tsn2 / div)
                    If sn2.Length = 5 Then
                        wpa_teletu = Sn1 & "Y" & sn2.PadLeft(7, "0")
                        Return wpa_teletu
                    Else
                        wpa_teletu = "NON CALCOLABILE"
                        Return wpa_teletu
                    End If
                End If
            End If
        Next
        wpa_teletu = "NO M.N DISPONIBILE"
        Return wpa_teletu
    End Function
End Module
